package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.TransferEntity;
import dongbai.soft.service.TransferService;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.R;
import dongbai.soft.utils.getUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;


/**
 * @author Kun
 * @email
 * @date 2023-06-21 14:58:04
 */
@RestController
@RequestMapping("")
public class TransferController {
    @Autowired
    private TransferService transferService;

    @Autowired
    private RedisTemplate redisTemplate;

    public static BigDecimal getRandom(BigDecimal min, BigDecimal max) {
        float minF = min.floatValue();
        float maxF = max.floatValue();

        //生成随机数
        BigDecimal db = new BigDecimal(Math.random() * (maxF - minF) + minF);

        //返回保留两位小数的随机数。不进行四舍五入
        return db.setScale(2, BigDecimal.ROUND_DOWN);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = transferService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 新建分站 ✔
     */
    @RequestMapping("/addT")
    public R addTransfer(@RequestBody Map<String, String> map) {
        TransferEntity transferEntity = new TransferEntity(getUUID.get6UUID(), map.get("name"), map.get("location"));
        if (transferService.getOne(new QueryWrapper<TransferEntity>().eq("name", transferEntity.getName())) != null) {
            return R.error();
        } else {
            List<BigDecimal> linedata = new ArrayList<>();
            List<Integer> bardata = new ArrayList<>();
            for (int i = 0; i < 14; i++) {
                bardata.add(0);
                linedata.add(new BigDecimal(0));
            }
            redisTemplate.opsForList().rightPushAll("Line:" + transferEntity.getTrid(), linedata);
            redisTemplate.opsForList().rightPushAll("Bar:" + transferEntity.getTrid(), bardata);
            transferService.save(transferEntity);
            return R.ok();
        }


    }

    /**
     * 修改分站信息
     */
    @RequestMapping("/edit")
    public R editTransfer(@RequestBody Map<String, String> map) {
        String trid = map.get("TRID");
        if (transferService.getOne(new QueryWrapper<TransferEntity>().eq("name", map.get("name")).ne("TRID", trid)) == null) {
            transferService.update(new UpdateWrapper<TransferEntity>().eq("TRID", trid)
                    .set("name", map.get("name")));
            return R.ok();
        } else {
            return R.error();
        }

    }

    @RequestMapping("/test")
    public void linebargenerator() {
        List<TransferEntity> trids = transferService.list();
        String TRID;
        for (TransferEntity transferEntity : trids) {
            TRID = transferEntity.getTrid();
            List<BigDecimal> linedata = new ArrayList<>();
            List<Integer> bardata = new ArrayList<>();
            Random df = new Random();
            for (int i = 0; i < 14; i++) {
                int randombar = df.nextInt(199);
                bardata.add(randombar);
                BigDecimal randomline = getRandom(new BigDecimal(5), new BigDecimal(20));
                randomline = randomline.multiply(new BigDecimal(randombar));
                linedata.add(randomline);
            }
            redisTemplate.opsForList().rightPushAll("Line:" + TRID, linedata);
            redisTemplate.opsForList().rightPushAll("Bar:" + TRID, bardata);
        }


    }

}
